package com.itheima.sms.sms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itheima.sms.sms.dto.BaseParamsDTO;
import com.itheima.sms.sms.dto.R;
import com.itheima.sms.sms.dto.SmsBatchParamsDTO;
import com.itheima.sms.sms.dto.SmsParamsDTO;
import com.itheima.sms.sms.service.SmsSendService;
import com.itheima.sms.sms.utils.HttpClientUtil;
import com.itheima.sms.sms.utils.SmsEncryptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class SmsSendServiceImpl implements SmsSendService {
    @Value("${itheima.sms.auth}")
    private boolean auth;
    @Value("${itheima.sms.domain}")
    private String domain;
    @Value("${itheima.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${itheima.sms.accessKeySecret}")
    private String accessKeySecret;

    private String send = "/sms/send";
    private String batchSend = "/sms/batchSend";

    /**
     * 单条短信
     *
     * @param smsParamsDTO
     * @return
     */
    @Override
    public R sendSms(SmsParamsDTO smsParamsDTO) {
        String url = domain + send;
        return send(smsParamsDTO, url);
    }

    /**
     * 批量短信
     *
     * @param smsBatchParamsDTO
     * @return
     */
    @Override
    public R batchSendSms(SmsBatchParamsDTO smsBatchParamsDTO) {
        String url = domain + batchSend;
        return send(smsBatchParamsDTO, url);
    }

    /**
     * 短信接收SDK: 通过HttpClient发送post请求，请求短信接收服务HTTP接口
     *
     * @param baseParamsDTO
     * @param url
     * @return
     */
    private R send(BaseParamsDTO baseParamsDTO, String url) {
        // 设置accessKeyId
        baseParamsDTO.setAccessKeyId(accessKeyId);
        if (auth) {
            if (StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(accessKeySecret)) {
                R.fail("accessKey 不能为空");
            }
        }
        baseParamsDTO.setTimestamp(String.valueOf(System.currentTimeMillis()));
        baseParamsDTO.setEncryption(SmsEncryptionUtils.encode(baseParamsDTO.getTimestamp(),
                baseParamsDTO.getAccessKeyId(), accessKeySecret));

        if (StringUtils.isBlank(domain)) {
            R.fail("domain 不能为空");
        }
        HttpClientUtil.HttpClientResult httpClientResult;
        try {
            httpClientResult = HttpClientUtil.doPost(url, JSONObject.toJSONString(baseParamsDTO));
            // 解析响应状态码
            if (httpClientResult.getCode() == 200) {
                log.info("httpRequest access success, StatusCode is:{}", httpClientResult.getCode());
                // 获取响应消息
                String responseContent = httpClientResult.getContent();
                log.info("responseContent is :" + responseContent);
                return JSONObject.parseObject(responseContent, R.class);
            } else {
                log.error("httpRequest access fail ,StatusCode is:{}", httpClientResult.getCode());
                return R.fail("status is " + httpClientResult.getCode());
            }

        } catch (IOException e) {
            log.error("error :", e);
            return R.fail(e.getMessage());
        }
    }
}
